package priv.conceit.study.book.multithread.chapter8.threadpool;

/**
 * study
 * 用于线程池内部
 * 此类会使用
 * @see RunnableQueue
 * 不断从queue中取出某个runnable,并运行
 *
 *
 * @author Conceit
 * 8/3/2022 增加方法
 * @since 1.0.0, 8/3/2022
 */
public class InternalTask implements Runnable{

	private final RunnableQueue runnableQueue;

	private volatile boolean running=true;

	public InternalTask(RunnableQueue runnableQueue){
		this.runnableQueue=runnableQueue;
	}


	/**
	 * When an object implementing interface <code>Runnable</code> is used
	 * to create a thread, starting the thread causes the object's
	 * <code>run</code> method to be called in that separately executing
	 * thread.
	 * <p>
	 * The general contract of the method <code>run</code> is that it may
	 * take any action whatsoever.
	 *
	 * @see Thread#run()
	 */
	@Override
	public void run() {
		//当前任务为running 并且没有被中断则将从queue中获取runnable 然后执行run方法
		while (running&&!Thread.currentThread().isInterrupted()){

				Runnable runnable=runnableQueue.pull();
				runnable.run();
//			try {
//			}catch (InterruptedException i){
//				running=false;
//				break;
//			}
		}
	}

	public void stop(){
		this.running=false;
	}
}
